<template>
  <view>
    <uinput
      placeholder="请输入规格一内容"
      :border="false"
      v-model="spec.spec1"
      :maxlength="20"
      :label="spec_title.spec1_title ? spec_title.spec1_title : '规格一标题'"
      :mustFill="false"
      v-if="spec_title.spec1_title"
    />
    <uinput
      placeholder="请输入规格二内容"
      :border="false"
      v-model="spec.spec2"
      :maxlength="20"
      :label="spec_title.spec2_title ? spec_title.spec2_title : '规格二标题'"
      :mustFill="false"
      v-if="spec_title.spec2_title"
    />
    <uinput
      placeholder="请输入市场价"
      :border="false"
      v-model="spec.market_price"
      :maxlength="20"
      type="number"
      label="市场价"
      :mustFill="false"
    />
    <uinput
      placeholder="请输入折扣价"
      :border="false"
      v-model="spec.platform_price"
      :maxlength="20"
      type="number"
      label="折扣价"
      :mustFill="false"
    />
    <uinput
      placeholder="请输入库存数量"
      :border="false"
      v-model="spec.amount"
      type="digit"
      label="库存"
      :mustFill="false"
    />
    <view style="padding-left: 30upx">此规格图片</view>
    <view class="upload" style="padding-left: 20upx">
      <uImg
        ref="upimg"
        :canUploadFile="true"
        :limit="limit1"
        :uploadFileUrl="uploadFileUrl"
        :uImgList.sync="uImgList1"
        @uploadSuccess="uploadSuccess1"
        :fileKeyName="name"
        @delete="delete_img1"
      />
    </view>
    <div class="">
      <van-button @tap="save_spec" size="large" color="#f95a4b"
        >保存规格</van-button
      >
      <van-button @tap="cancle_save_spec" size="large">返回</van-button>
    </div>
  </view>
</template>

<script>
import uImg from "../../components/zhtx-uploadImg/zhtx-uploadImg";
export default {
  props: {},
  components: {
      uImg
  },
  data() {
    return {
      spec: {
        spec1: "",
        spec2: "",
        market_price: "",
        platform_price: "",
        amount: "",
        img_url: "",
      }, // 规格参数
      spec_title: {
        spec1_title: "",
        spec2_title: "",
      },
      uploadFileUrl:
        "https://api.comm.t.xingkonglian.net:8098/api/v1/oss/upload_starx",
      name: "file",
      uImgList1: [],
      limit1: 1,
      imgList1: [],
      check:false
    };
  },
  computed: {},
  watch: {},
  onLoad(options) {
      if(options.spec_title) {
          this.spec_title= JSON.parse(options.spec_title)
      }
      if(options.check) {
          console.log(options.spec);
          this.check=true
          this.check_index=options.index
          this.spec = JSON.parse(options.spec)
          this.uImgList1.push(options.image)
      }
  },
  onReady() {},
  onShow() {},
  methods: {
    uploadSuccess1(result) {
      if (result.statusCode == 200) {
        //上传成功的回调处理
        console.log(result.data);
        const img = JSON.parse(result.data).data;
        console.log(img);
        this.imgList1.push(img);
        this.spec.img_url = this.imgList1[0];
      } else {
      }
    },
    delete_img1(index) {
      console.log(index);
      this.imgList1.splice(index, 1);
      console.log(this.imgList1);
    },
    save_spec() {
      if (
        this.spec.spec1 == "" ||
        this.spec.spec2 == "" ||
        this.spec.market_price == "" ||
        this.spec.platform_price == "" ||
        this.spec.amount == "" ||
        this.spec.img_url == ""
      ) {
        return uni.showToast({
            title: '请输入完整数据',
            duration: 2000,
            icon:'none'
        });
      }
      this.spec.amount = Number(this.spec.amount);
      this.spec.market_price = Number(this.spec.market_price);
      this.spec.platform_price = Number(this.spec.platform_price);
        let pages =  getCurrentPages();
        let prevpage = pages[pages.length-2]
        console.log(this.spec);
        console.log(this.check);
        console.log(prevpage);
      if (this.check) {
          prevpage.$vm.param.spec_detail[this.check_index] = this.spec;
      } else {
          prevpage.$vm.param.spec_detail.push(this.spec);
          console.log(prevpage.param);
      }
      // Object.assign(prevpage.param.spec_title, prevpage.spec_title);
        uni.navigateBack({
             delta: 1
        });
    },
    cancle_save_spec() {
     uni.navigateBack({
          delta: 1
     });
    },
  },
  onReachBottom() {},
  onHide() {},
};
</script>

<style scoped lang="scss" scoped>
</style>
